﻿// -----------------------------------------------------------------------
// <copyright file="EmailHelper.cs" company="None">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ODataTestFrameworkCore.shared
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Outlook = Microsoft.Office.Interop.Outlook;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class EmailHelper
    {
        public static bool SendEmail(string to, string subject, string messageBody)
        {
            bool success = true;
            try
            {
                // Create the Outlook application.
                Outlook.Application oApp = new Outlook.Application();

                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

                oMsg.HTMLBody = messageBody;

                // String sDisplayName = "MyAttachment";
                // int iPosition = (int)oMsg.Body.Length + 1;
                // int iAttachType = (int)Outlook.OlAttachmentType.olByValue;

                // Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);

                oMsg.Subject = subject;

                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip;
                foreach (string item in to.Split(','))
                {
                    oRecip = (Outlook.Recipient)oRecips.Add(item);
                    oRecip.Resolve();
                }
                
                // Send.
                oMsg.Send();
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oApp = null;
            }//end of try block
            catch (Exception)
            {
                success = false;
            }

            return success;
        }
    }
}
